import { persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage'

import { LOAD_SELECTED_ID, DELETE_SELECTED_ID } from '../actions/LiveFeedActions';

const initialState = {
    error: null,
    selectId: []
}

const persistConfig = {
    key: 'selectId',
    storage,
    whitelist: ['selectId'], // place to select which state you want to persist
}

const liveFeedReducer = (state = initialState, action) => {
    switch (action.type) {
        case LOAD_SELECTED_ID:
            if (Array.isArray(action.data)) {
                return {
                    ...state,
                    selectId: action.data,
                }
            } else {
                let selectIds = [...state.selectId]
                let selectIdsFilter = []
                if (selectIds.some(item => item == action.data)) {
                    selectIdsFilter = selectIds.filter(item => item != action.data)
                } else {
                    selectIdsFilter = [...selectIds, action.data]
                }
                return {
                    ...state,
                    selectId: selectIdsFilter,
                }
            }

        case DELETE_SELECTED_ID:
            return {
                ...state,
                selectId: [],
            }
        default:
            return { ...state };
    }
};

export default persistReducer(persistConfig, liveFeedReducer);